package com.atguigu.yygh.user.controller;

import com.atguigu.yygh.common.R;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author zhmwi
 * @Date 2021/7/19
 * @Dersciption
 */
@Api("获取就诊人列表")
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    private PatientService patientService;

    //查询就诊人列表
    @ApiOperation("查询就诊人列表")
    @GetMapping("auth/findAll")
    public R getPatientList(HttpServletRequest request){
        //获取当前用户id
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> patientList = patientService.getPatientList(userId);
        return R.ok().data("list",patientList);
    }

    //根据就诊人id查询就诊人信息
    @ApiOperation("获取就诊人信息")
    @GetMapping("auth/get/{id}")
    public R getPatientInfo(@PathVariable Long id){
        Patient patient = patientService.getPatientInfo(id);
        System.err.println("patient -- "+patient);
        return R.ok().data("patient",patient);
    }

    //添加就诊人
    @ApiOperation(value = "添加就诊人")
    @PostMapping("auth/save")
    public R savePatient(@RequestBody Patient patient,
                         HttpServletRequest request) {
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.savePatient(patient);
        return R.ok();
    }
    //修改就诊人
    @PostMapping("auth/update")
    public R updatePatient(@RequestBody Patient patient) {
        patientService.updatePatientById(patient);
        return R.ok();
    }

    //删除就诊人
    @DeleteMapping("auth/remove/{id}")
    public R removePatient(@PathVariable Long id) {
        patientService.removePatientById(id);
        return R.ok();
    }
    //获取就诊人 远程调用
    @ApiOperation(value = "获取就诊人")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(
            @PathVariable("id") Long id) {
        Patient patient = patientService.getPatientInfo(id);
        return patient;
    }


}
